package com.jpz.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jpz.bean.Course;
import com.jpz.bean.Grade;
import com.jpz.service.CourseService;
import com.jpz.service.GradeService;
import com.jpz.utils.pageshow;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jpz
 * @since 2018-11-07
 */
@Controller
@RequestMapping("/grade")
public class GradeController {
	@Autowired
	GradeService gradeService;
	@Autowired
	CourseService courseser;
	@RequestMapping("/show")
	public String show(Model model,int pageindex) {
		Page<Grade> page = new Page<>(pageindex, 4);
		Page<Grade> lis = gradeService.selectPage(page, new EntityWrapper<Grade>().eq("del", 0));
		List<Grade> list = lis.getRecords();
		for (Grade grade : list) {
			Course course2 = courseser.selectById(grade.getCid());
			grade.setCourse(course2);
		}
		long total = lis.getTotal();
		int tol = new Long(total).intValue();
		pageshow<Grade> pagebean = new pageshow<>(list, tol, null, lis.getCurrent());
		model.addAttribute("pagebean",pagebean);
		return "/gradelist.jsp";
	}
	@RequestMapping("/goup")
	public String goup(Model model,int gid) {
		List<Course> list = courseser.selectList(null);
		Grade grade = gradeService.selectById(gid);
		model.addAttribute("grade", grade);
		model.addAttribute("course",list);
		return "/gradeupdate.jsp";
	}
	@RequestMapping("/update")
	public String update(Model model,Grade grade) {
		boolean up = gradeService.updateById(grade);
			return "redirect:/grade/show?pageindex=1";
	}
	@RequestMapping("/del")
	public String del(Model model,int gid) {
		Grade grade = gradeService.selectById(gid);
		grade.setDel(1);
		boolean updateById = gradeService.updateById(grade);
		return "redirect:/grade/show?pageindex=1";
	}
	@RequestMapping("/add")
	public String add(Model model) {
		List<Course> clist = courseser.selectList(null);
		model.addAttribute("cour", clist);
		return "/gradeadd.jsp";
	}
	@RequestMapping("/radd")
	public String radd(Model model,Grade grade) {
		gradeService.insert(grade);
		return "redirect:/grade/show?pageindex=1";
	}
}

